<?php 
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}
if(isset($_POST['act'])||isset($_GET['act'])){
	include_once 'public/DB_Oracle.php';
	include_once 'public/Json.php';
	$DBsql = new DB_Oracle ();
	$act=(!empty($_POST['act']))?$_POST['act']:$_GET['act'];
	if ($act=='select') {//查询某警员相关信息，用于更新警员信息
		
	}else if($act=='valid'){//新增警员，先验证警号是否可用
		$name=$_POST['param'];//用户名
		$sql="select * from GPS_SYS_PEOPLE sa where sa.LOGIN_NAME = '$name'";
		$row=$DBsql->fetch_one_array($sql);
		if(isset($row['USER_ID'])){
			echo '警号已经存在，请更换！';
		}else{
			echo 'y';
		}
	}elseif ($act=='selectArea'){//查询某个区域下的所有派出所
		$sn=$_POST['sn'];
		$sql="select *
			  from dep_department dd
			 where dd.area_index = '$sn'
			 order by dd.department_index";
		$areas=$DBsql->fetch_all_array($sql);
		$str="";
		echo JSON($areas);
	}else if($act=='add'){//新增警员
		$userNum=$_POST['userNum'];          //警员用户名
		$userName=$_POST['userName'];		 //警员真实姓名
		$userPwd=$_POST['userPwd'];			 //警员密码
		$userPwdMd5=md5($userPwd);			 //加密警员密码
		$userPho=$_POST['userPho'];			 //警员手机号
		$userGroup=$_POST['userGroup'];		//警员所在用户组
		$area=$_POST['area'];				//警员所在区域
		$work=$_POST['work'];				//警员所在派出所
		$phoneNum=$_POST['phoneNum'];//警员编号
		$sql="insert into sys_admin
					  (user_index,
					   user_id,
					   user_psw,
					   user_name,
					   department_index,
					   user_tel,
					   user_sn,
					   permission_index)
					values
					  (SYS_ADMIN_SEQ.NEXTVAL,
					   '$userNum',
					   '$userPwdMd5',
					   '$userName',
					   '$work',
					   '$userPho',
					   '$phoneNum',
					   '$userGroup')";
	
	if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='up'){
		$userNum=$_POST['userNumUp'];          //警员用户名
		$userName=$_POST['userNameUp'];		 //警员真实姓名
		$userPwd=$_POST['userPwdUp'];			 //警员密码
		$userPwdMd5=md5($userPwd);			 //加密警员密码
		$userPho=$_POST['userPhoUp'];			 //警员手机号
		$userGroup=$_POST['userGroupUp'];		//警员所在用户组
		$area=$_POST['areaUp'];				//警员所在区域
		$work=$_POST['workUp'];				//警员所在派出所
		$phoneNum=$_POST['phoneNumUp'];//警员编号	
		$index=$_POST['hiddUp'];//管理员ID
		$sql="update sys_admin
					   set user_id       = '$userNum',
					       user_psw         = '$userPwdMd5',
					       user_name        = '$userName',
					       department_index = '$work',
					       user_tel         = '$userPho',
					       user_sn          = '$phoneNum',
					       permission_index = '$userGroup'
					 where user_index = '$index'";
		if($DBsql->query($sql)){
				echo '{
					"info":"数据更新成功！",
					"status":"y"
				}';
			}else{
				echo '{
					"info":"数据更新失败！",
					"status":"n"
				}';
			}
	}else if($act=='del'){//删除警员
		$sn=$_POST['sn'];
		$sql="delete from sys_admin sa where sa.user_index in ($sn)";
		$DBsql->query($sql);
		echo 'y';
	}
}
?>